package com.wv.api.service.basic.impl;

import cn.hutool.extra.template.engine.wit.WitTemplate;
import com.wv.api.mapper.basic.BasicBuildingStructureDao;
import com.wv.api.service.basic.IBasicBuildingStructureService;
import com.wv.common.result.Result;
import com.wv.model.basic.BasicBuildingStructure;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BasicBuildingStructureServiceImpl implements IBasicBuildingStructureService {

    @Autowired
    private BasicBuildingStructureDao basicBuildingStructureDao;

    @Override
    public Result getTreeData() {
        // 实现获取树形数据逻辑
        List<BasicBuildingStructure> basicBuildingStructures = basicBuildingStructureDao.selectList(null);
        List<BasicBuildingStructure> collect = basicBuildingStructures.stream().filter(item -> item.getParentStructureId().equals("0"))
                .map(item -> item.setChildren(getChildren(basicBuildingStructures, item.getId())))
                .sorted(Comparator.comparing(item -> (item.getStructureLevel() == null ? 0 : item.getStructureLevel())))
                .collect(Collectors.toList());
        return Result.ok(collect);
    }

    private List<BasicBuildingStructure> getChildren(List<BasicBuildingStructure> list, String parentId) {
        return list.stream().filter(item -> item.getParentStructureId().equals(parentId))
                .map(item -> item.setChildren(getChildren(list, item.getId())))
                .sorted(Comparator.comparing(item -> (item.getStructureLevel() == null ? 0 : item.getStructureLevel())))
                .collect(Collectors.toList());
    }
}
